package com.sample.wanandroidx.ui.test

import com.sample.library.util.ViewUtil.setOnProxyClickListener
import com.sample.wanandroidx.basic.BaseActivity
import com.sample.wanandroidx.databinding.ActivityDatetimepickerBinding
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog
import java.util.*

class DatetimepickerActivity : BaseActivity<ActivityDatetimepickerBinding>(ActivityDatetimepickerBinding::inflate),
                               TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {

    override fun initialization() {
        setTitleStr(intent.getStringExtra("title"))
        binding.selectTime.setOnProxyClickListener {
            val now: Calendar = Calendar.getInstance()
            val tpd = TimePickerDialog.newInstance(this, now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), true)
            tpd.show(supportFragmentManager, "Timepickerdialog")
        }
        binding.selectData.setOnProxyClickListener {
            val now: Calendar = Calendar.getInstance()
            val dpd = DatePickerDialog.newInstance(this, now.get(Calendar.YEAR),  // Initial year selection
                now.get(Calendar.MONTH),  // Initial month selection
                now.get(Calendar.DAY_OF_MONTH) // Inital day selection
            )
            dpd.show(supportFragmentManager, "Datepickerdialog")
        }
    }

    override fun onTimeSet(view: TimePickerDialog?, hourOfDay: Int, minute: Int, second: Int) {
        val hourString = if (hourOfDay < 10) "0$hourOfDay" else "" + hourOfDay
        val minuteString = if (minute < 10) "0$minute" else "" + minute
        val secondString = if (second < 10) "0$second" else "" + second
        val time = "You picked the following time: " + hourString + "h" + minuteString + "m" + secondString + "s"
        binding.text.setText(time)
    }

    override fun onDateSet(view: DatePickerDialog?, year: Int, monthOfYear: Int, dayOfMonth: Int) {
        val date = "You picked the following date: " + dayOfMonth.toString() + "/" + (monthOfYear + 1).toString() + "/" + year
        binding.text.setText(date)
    }

}